/*
 * Copyright (C) 2010 EPRO
 */
package com.epro.dx.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.fragment.ProductListFragment;
import com.weipu.common.constants.Constant;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.impl.ProductServiceProviderImpl;
import com.weipu.common.facade.model.CatelogModel;
import com.weipu.common.util.JsonFormat;
import com.weipu.common.util.Logger;

import java.util.ArrayList;

/**
 * 商品列表展示Activity 实现商品列表展示，并可按指定的方式进行排序
 *
 * @author cw55072
 * @version [1.0, 2010-09-02]
 */
public class ProductListActivity extends BaseFragmentActivity {

    private SimpleFragmentPagerAdapter pagerAdapter;

    private ViewPager viewPager;

    private TabLayout tabLayout;

    ArrayList<CatelogModel> categoryList;
    private String fromWhere;
    private String productListType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_productlist_tab);

        initTitle(savedInstanceState);

        fromWhere = getIntent().getStringExtra(Constant.TO_PRODUCTLIST_FROM);
        productListType = getIntent().getStringExtra(Constant.PRODUCTLIST_TYPE);


        if (Constant.FROM_HOME.equals(fromWhere)
                || Constant.FROM_HOME_BFD.equals(fromWhere)) {
            setupTabLayout();
        } else {
            setupContent();
        }
    }

    private void setupContent() {
        findViewById(R.id.tab_parent).setVisibility(View.GONE);
        findViewById(R.id.content).setVisibility(View.VISIBLE);

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content, new ProductListFragment(), "ProductListFragment").commit();

    }

    private void setupTabLayout() {
        categoryList = ProviderFactory.createCategoryServiceDBProvider().queryCategoryListByParentCategoryID(ProductServiceProviderImpl.TOP_CATEGORY_DEFAULT_PARENT_ID);
        //遗留问题，当网络连接断开进入这个页面的时候，会出现空指针异常
        if (categoryList == null) return;

        StringBuffer sb = new StringBuffer();
        for (CatelogModel catelogModel : categoryList) {
            if (sb.length() > 0) {
                sb.append("-");
            }
            sb.append(catelogModel.getBrowseID());
        }

        CatelogModel all = new CatelogModel();
        all.setBrowseID(-3);
        all.setShortName("All");
        categoryList.add(0, all);

        pagerAdapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager(), this);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(1);
        tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        if (false) {
            //TODO US仓版本修改  这个需求有待确认，如果是从首页的US仓点击进来的，则隐藏掉上面的分类
            //if(Constant.FROM_HOME.equals(fromWhere) && Constant.TYPE_WAREHOUSE.equals(productListType))
//            tabLayout.setVisibility(View.GONE);
        } else {
            tabLayout.setupWithViewPager(viewPager);
            for (int i = 0; i < categoryList.size(); i++) {
                TabLayout.Tab tab = tabLayout.getTabAt(i);

                tab.setCustomView(R.layout.activity_productlist_tab_item);
                TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tv_sliding_tabs_item);
                textView.setText(categoryList.get(i).getShortName());
                textView.setTag(i);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem((Integer) v.getTag());
                    }
                });
            }

            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tv_sliding_tabs_item);
                    textView.setTextColor(getResources().getColor(R.color.myorder_tab_text_selected));
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tv_sliding_tabs_item);
                    textView.setTextColor(Color.parseColor("#5d5d5d"));
                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });
        }



    }

    private void initTitle(Bundle outState) {
        setLeftBtnContent(true, -1);

        Intent intent = getIntent();
        String catalogName = "";
        if (null != intent) {
            catalogName = intent.getStringExtra(Constant.PRODUCT_TITLE);
        } else if (null != outState) {
            catalogName = outState.getString(Constant.PRODUCT_TITLE);
        }

        if (null != catalogName) {
            setTitleContent(catalogName);
        } else {
            setTitleContent(getString(R.string.product_list));
        }
    }

    public class SimpleFragmentPagerAdapter extends FragmentStatePagerAdapter {


        public SimpleFragmentPagerAdapter(FragmentManager fm, Context context) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return ProductListFragment.getInstance(categoryList.get(position).getBrowseID());
        }

        @Override
        public int getCount() {
            return categoryList.size();
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return categoryList.get(position).getShortName();
        }
    }


}